<?php

/**
 * Pwiff - PHP SWF Library
 *
 * LICENSE
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @package    Pwiff
 * @subpackage Records
 * @copyright  Copyright (c) 2010 Steven Harris
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: FillStyleArray.php 7 2010-02-17 20:43:45Z indigo.sunrise $
 */

/**
 * FillStyleArray record.
 *
 * @package    Pwiff
 * @subpackage Records
 */
class Pwiff_Record_FillStyleArray extends Pwiff_Record
{
	/**
	 * Array of FillStyle objects.
	 *
	 * @var array
	 */
	protected $fillStyles = array();
	
	/**
	 * DefineShape version this record is being used with.
	 * 
	 * @var int
	 */
	protected $shapeVersion = 1;
	
	/**
	 * Sets the array of fill styles.
	 * 
	 * @param  array $fillStyles
	 * @return Pwiff_Record_FillStyleArray
	 */
	public function setFillStyles(array $fillStyles)
	{
		// make sure there aren't too many styles for the version
		if ($this->shapeVersion === 1 AND sizeof($fillStyles) > 255) {
			throw new Pwiff_Exception('FillStyleArray with DefineShape version 1 can only store 255 styles.');
		}
		
		$this->fillStyles = $fillStyles;
		return $this;
	}
	
	/**
	 * Adds a fill style to the array.
	 *
	 * @param  Pwiff_Record_FillStyle $style
	 * @return Pwiff_Record_FillStyleArray
	 */
	public function addFillStyle(Pwiff_Record_FillStyle $style)
	{
		// make sure we don't get more than 255 styles for version 1
		if ($this->shapeVersion === 1 AND sizeof($this->fillStyles) >= 255) {
			throw new Pwiff_Exception('FillStyleArray with DefineShape version 1 can only store 255 styles.');
		}
		
		$this->fillStyles[] = $style;
		return $this;
	}
	
	/**
	 * Sets the DefineShape version.
	 * 
	 * @param  int $version
	 * @return Pwiff_Record_FillStyleArray
	 */
	public function setShapeVersion($version)
	{
		$this->shapeVersion = $version;
		return $this;
	}
	
	/**
	 * Gets the array of fill styles.
	 * 
	 * @return array
	 */
	public function getFillStyles()
	{
		return $this->fillStyles;
	}
	
	/**
	 * Gets the number of fill styles.
	 * 
	 * @return int
	 */
	public function getFillStyleCount()
	{
		return sizeof($this->fillStyles);
	}
	
	/**
	 * Gets the DefineShape version.
	 * 
	 * @return int
	 */
	public function getShapeVersion()
	{
		return $this->shapeVersion;
	}
	
	/**
	 * Gets the length of this record.
	 * 
	 * @return int
	 */
	public function getRecordLength()
	{
		$length = 1;
		if ($this->shapeVersion >= 2 AND sizeof($this->fillStyles) > 254) {
			$length += 2; // add UI16 for extended count
		}
		
		foreach ($this->fillStyles AS $style) {
			$length += $style->getRecordLength();
		}
		
		return $length;
	}
	
	/**
	 * Writes the FillStyleArray record to the given output instance.
	 *
	 * @param  Pwiff_Output $out
	 */
	public function write(Pwiff_Output $out)
	{
		$numStyles = sizeof($this->fillStyles);
		
		// if shape version is 1, or we have < 255 styles, just write a UI8
		if ($this->shapeVersion === 1 OR $numStyles < 255) {
			$out->writeUI8($numStyles);
		}
		else {
			// if the version is >= 2 and we have >= 255 styles, extend it
			$out->writeUI8(255);
			$out->writeUI16($numStyles);
		}
		
		// write each style
		foreach ($this->fillStyles AS $style) {
			$style->write($out);
		}
	}
}
